# %%
import os

from matplotlib import pyplot as plt

from common.geoimage.raster_dataset import RasterDataset
from common.rs_utils.bitmask import interpret_bitmask

from catalog.models.landsat8_l2 import Landsat8L2Scene

# %%
items = Landsat8L2Scene.query_many_items()
items = list(items)

print(len(items))
print(items[0])

# %%

item = items[0]
print(item.uid, item.start_datetime, item.cloud_cover)
# %%
fpath_bqa = item.info["assets"]["BQA"]["href"]
print(os.path.exists(fpath_bqa))

# NOTE: not correctly path and need to work with LIU Hongchen to correct this
# let us just use a known file path for now

# %%
fpath_bqa = (
    "/NAS5/agri/hongchen/landsat8_l2/P045/P045R034/LC08_L1TP_045034_20200105_20200113_01_T1"
    "LC08_L1TP_045034_20200105_20200113_01_T1_BQA.tif"
)

rds = RasterDataset.from_file(fpath_bqa)
print(rds.meta)

# %%
nodata_mask = interpret_bitmask(rds.data[0, :, :], 0, 1)
cloud_mask = interpret_bitmask(rds.data[0, :, :], 3, 1)
# %%
plt.imshow(cloud_mask)
# %%
plt.imshow(nodata_mask)
# %%
